package root.service;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import root.utils.StringHelper;

import java.util.List;

/**
 * 菜单service
 * @author 吕梁山
 * @date 2018/9/25
 */
public class MenuService {

    public List<Record> getMenuList(){
        List<Record> list = Db.find("select * from t_manager_menu m where m.status = 1 and m.parent_id = 1 and m.del_flag = 0");
        for (Record record : list) {
            Integer id = record.getInt("id");
            List<Record> list1 = Db.find("select * from t_manager_menu where status = 1 and parent_id = " + id + " and del_flag = 0");
            if(list1!=null && list1.size()>0){
                record.set("childlist",list1);
            }
        }
        return list;
    }

    /**
     * 分页获取菜单
     */
    public Page<Record> getMenuPage(String name, Integer pageNumber, int pageSize) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("select m.*,if(m.parent_id=1,'首页',(select pm.name from t_manager_menu pm where pm.id = m.parent_id)) parentMenuName from t_manager_menu m where m.del_flag = 0 ");
        if(StringHelper.isNotEmpty(name)){
            stringBuilder.append(" and m.name = '" + name + "'");
        }
        stringBuilder.append(" order by id");
        return Db.paginate(pageNumber, pageSize, new SqlPara().setSql(stringBuilder.toString()));
    }

    /**
     * 获取一级菜单
     */
    public List<Record> getFirstMenu() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("select id,name from t_manager_menu where parent_id = 1 and status = 1 and del_flag = 0");
        return Db.find(stringBuilder.toString());
    }

    /**
     * 添加菜单
     */
    public void saveMenu(Record menu) {
        Db.save("t_manager_menu", menu);
    }

    /**
     * 更新菜单
     */
    public void updateMenu(Record menu) {
        Db.update("t_manager_menu",menu);
    }

    public Record getMenu(String id) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("select * from t_manager_menu where id = " + id + " and del_flag = 0");
        return Db.findFirst(stringBuilder.toString());
    }

    /**
     * 删除菜单
     */
    public int deleteMenu(String id) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("update t_manager_menu set del_flag = 1 where id = " + id);
        return Db.update(stringBuilder.toString());
    }

    /**
     * 获取子菜单
     */
    public List<Record> getChildrenList(String menuId){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("select * from t_manager_menu where del_flag = 0 and parent_id = " + menuId);
        return Db.find(stringBuilder.toString());
    }

    public Record getMaxIdByParentId(int parentMenu) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("select id from t_manager_menu where parent_id = " + parentMenu + " and id != 9000 order by id desc limit 1");
        return Db.findFirst(stringBuilder.toString());
    }
}
